package com.ccz.demo.bookdemo.javayibubianchengshizhan.c2.chapter3;

import com.ccz.demo.bookdemo.javayibubianchengshizhan.c2.DoSomething;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class AsyncFutureExample {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
         // 1.创建一个Future任务
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            return DoSomething.doSomethingC();
        });
        // 2.开启异步单元执行任务c
        Thread thread = new Thread(futureTask, "Threadc");
        thread.start();

        // 3.执行任务D
        String s = DoSomething.doSomethingD();

        // 4.同步等待线程C结束
        String s1 = futureTask.get();
        // 5.打印两个结果
        System.out.println(s1 + "" + s);
        System.out.println(System.currentTimeMillis() - start);
    }


}
